package com.fs.service.Impl;

import com.fs.dto.ProductGalleryAddDTO;
import com.fs.entity.ProductGallery;
import com.fs.mapper.ProductGalleryMapper;
import com.fs.service.ProductGalleryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 商品图片业务实现
 */
@Service
@Slf4j
public class ProductGalleryServiceImpl implements ProductGalleryService {

    @Autowired
    private ProductGalleryMapper galleryMapper;

    @Override
    public void add(ProductGalleryAddDTO dto) {
        ProductGallery g = new ProductGallery();
        BeanUtils.copyProperties(dto, g);
        g.setCreateTime(LocalDateTime.now());
        galleryMapper.insert(g);
        log.info("[ProductGalleryService] 添加图片 productId={}", dto.getProductId());
    }

    @Override
    public void update(ProductGallery gallery) {
        // 允许修改 image/text/sort；updateById 可为动态更新
        gallery.setCreateTime(null); // 不允许更新 createTime
        galleryMapper.updateSelective(gallery);
        log.info("[ProductGalleryService] 更新图片 id={}", gallery.getId());
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) return;
        galleryMapper.deleteByIds(ids);
        log.info("[ProductGalleryService] 删除图片 ids={}", ids);
    }

    @Override
    public List<ProductGallery> listByProductId(Long productId) {
        return galleryMapper.selectByProductId(productId);
    }

    @Override
    public void deleteByProductIds(List<Long> productIds) {
        if (productIds == null || productIds.isEmpty()) return;
        galleryMapper.deleteByProductIds(productIds);
        log.info("[ProductGalleryService] 删除商品相关图片 productIds={}", productIds);
    }
}
